<?php
class Post_Model extends CI_Model{
	
	
	 function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	
	function getData($limit, $offset,$judul=""){
		$sql="SELECT * from tbl_post where judul like '%".$judul."%' order by id_post desc limit $offset, $limit";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	
	function get_detail_data($id){
		$sql="SELECT * from tbl_post where id_post='".$id."'";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->row_array();
		}else{
			return array();
		}
	}
	
	function getAllData(){
		$sql="SELECT * from tbl_post order by id_post desc";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			return $query->result_array();
		}else{
			return array();
		}
	}
	function count_all_num_rows($judul=""){
		$sql="SELECT * from tbl_post where judul like '%".$judul."%' ";
		//echo $sql;
		$query=$this->db->query($sql);
		return $query->num_rows();
		
	}
	
	function insert_data($judul,$gambar,$slug,$konten,$pilihan_sidebar,$tags){
		
		$data=array('judul'=>$judul,
					'slug'=>$slug,
					'pilihan_sidebar'=>$pilihan_sidebar,
					'konten'=>$konten,
					'tags'=>$tags,
					'tgl_posting'=>date('Y-m-d'),
					'gambar'=>$gambar);
		$insert=$this->db->insert('tbl_post',$data);
		return $insert;
	}
	
	function update_data($id,$judul,$gambar,$konten,$pilihan_sidebar,$slug,$tags){
		$data=array('judul'=>$judul,
					'pilihan_sidebar'=>$pilihan_sidebar,
					'slug'=>$slug,
					'konten'=>$konten,
					'tags'=>$tags,
					'gambar'=>$gambar);
		$this->db->where('id_post',$id);
		$update=$this->db->update('tbl_post',$data);
		return $update;
	}
	
	function delete_data($id){
		$data=array('id_post'=>$id);
		$this->db->where($data);
		$delete=$this->db->delete('tbl_post'); 
		return $delete;
	}
	
	function create_slug($title){
		$url_title=url_title($title);
		$sql="SELECT * from tbl_post where slug='".$url_title."'";
		//echo $sql;
		$query=$this->db->query($sql);
		if ($query->num_rows()>0){
			$acak=rand(0000,9999);
			return strtolower($url_title."-".$acak);
		}else{
			return strtolower($url_title);
		}
	}
	

}
?>